//
//  HLJSeletionViewController.m
//  B段项目
//
//  Created by 111111 on 15/12/22.
//  Copyright © 2015年 张智超. All rights reserved.
//

#import "HLJSeletionViewController.h"

#import "HLJHomeHeaderView.h"       /**<头视图View  轮播图*/
#import "HLJTopicCell.h"            /**<Topic 标题 图片cell*/
#import "HLJLabelCell.h"            /**<Label 没有标题的 cell*/
#import "HLJSoundCell.h"            /**<Sound 大播放按钮的 cell*/
#import "HLJIllustrationCell.h"     /**<插画 三图 cell*/
#import "HLJMusicCell.h"            /**<Music  cell*/
#import "HLJWorldCell.h"            /**<World 图 标题 内容 cell*/
#import "HLJDetailViewController.h" /**<详情 controllerView*/
#import "HLJPhotoViewController.h"  /**<相册 */
#import "HLJHomeModel.h"
#import "TOTools.h"
#import "TORadioDetailViewController.h"
#import "ZZCDetailViewController.h"
#import "ZZCShopViewController.h"
#import "TOAnimatedView.h"
@interface HLJSeletionViewController ()<UITableViewDataSource,UITableViewDelegate,HLJillCellDeleage,headerDelegate,TOAnimatedViewDelegate, UMSocialUIDelegate>

@property(nonatomic, retain)UITableView *boutTableView;    /**<精品tableV*/
@property(nonatomic, retain)HLJHomeHeaderView *headerView;  /**<Home 头视图*/
@property(nonatomic, retain)NSMutableArray *allArray;      /**<所有model数组*/
@property(nonatomic, retain)NSMutableArray *playListArray; /**<model中playlist数组*/
@property(nonatomic, assign) CGFloat cellHeight;
@property(nonatomic, assign)NSInteger nextPage;
@property(nonatomic, assign)BOOL isUpLoad;
@property(nonatomic,strong)TOAnimatedView *animouteView;


@end

@implementation HLJSeletionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
        //图标
    UIImageView *img = [[UIImageView alloc]init];
        img.image = [UIImage imageNamed:@"iconfont-shouyetu.png"];
               [self.rightView addSubview:img];
    
    
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(- 30);
        make.size.mas_equalTo(CGSizeMake(WIDTH/16, WIDTH/16));
        make.top.mas_equalTo(10);
        
        
    }];

    
    
    //初始化
    self.allArray = [NSMutableArray array];
    self.playListArray = [NSMutableArray array];
    
   
    self.isUpLoad = NO;
    
#pragma mark  创建tableView
    
    self.boutTableView = [[UITableView alloc]init];
     [self.view addSubview:self.boutTableView];
    
    [self.boutTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(self.view);
        
        
    }];

    self.boutTableView.delegate = self;
    self.boutTableView.dataSource = self;
   
    
#pragma mark 注册
    /**<Topic 标题 图片 cell*/
    [self.boutTableView registerClass:[HLJTopicCell class] forCellReuseIdentifier:@"topicCell"];
    /**<Label 没有标题的 cell*/
    [self.boutTableView registerClass:[HLJLabelCell class] forCellReuseIdentifier:@"labelCell"];
    /**<Sound 大播放按钮的 cell*/
    [self.boutTableView registerClass:[HLJSoundCell class] forCellReuseIdentifier:@"soundCell"];
    /**<插画 三图 cell*/
    [self.boutTableView registerClass:[HLJIllustrationCell class] forCellReuseIdentifier:@"illCell"];
    /**<Music  cell*/
    [self.boutTableView registerClass:[HLJMusicCell class] forCellReuseIdentifier:@"musicCell"];
    /**<World 图 标题 内容 cell*/
    [self.boutTableView registerClass:[HLJWorldCell class] forCellReuseIdentifier:@"worldCell"];
    
#pragma mark 轮播图
    
    //创建tableView头视图
    self.headerView = [[HLJHomeHeaderView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/4)];
     self.headerView.HeaderDelegate = self;
    
    self.boutTableView.tableHeaderView = self.headerView;
    
    
   
    
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    
    
    
    
    
    
    //调用请求数据
    [self requestData];
    [self getDataForJING];
    //调用上下拉刷新
//    [self addFooterRefresh];
//    [self addHeaderRefresh];
    
    MJRefreshGifHeader *header = [TOTools createTableViewGifHeaderWithTarget:self Action:@selector(addHeaderRefresh)];
    self.boutTableView.mj_header = header;
    [self.boutTableView.mj_header beginRefreshing];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addFooterRefresh)];
    self.boutTableView.mj_footer = footer;
    
//
    self.animouteView = [TOAnimatedView shareAnimatedView];
    self.animouteView.animatedViewDelegate = self;
    [self.view addSubview:self.animouteView];
}

#pragma mark - 弹出分享view
-(void)presentShareViewWithContent:(NSMutableDictionary *)shareContent
{
    
    NSData *shareImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[shareContent objectForKey:@"pic"]]];
    UIImage *shareImage = [UIImage imageWithData:shareImageData];
    
    
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"568342bc67e58e851f0019a2"
                                      shareText:[shareContent objectForKey:@"text"]
                                     shareImage:shareImage
                                shareToSnsNames:@[UMShareToWechatSession,UMShareToWechatTimeline,UMShareToWechatFavorite, UMShareToDouban, UMShareToQQ, UMShareToQzone]
                                       delegate:self];
    
}

- (void)presentHeaderID:(NSString *)headerID HeaderName:(NSString *)name Url:(NSString *)url{
    
    
    HLJDetailViewController *detail = [[HLJDetailViewController alloc]init];
    TORadioDetailViewController *to = [[TORadioDetailViewController alloc]init];
    ZZCDetailViewController *zzc = [[ZZCDetailViewController alloc]init];
    ZZCShopViewController *shop = [[ZZCShopViewController alloc]init];
    if ([name isEqualToString:@"fm"]) {
        to.radioId = headerID;
        [self.navigationController pushViewController:to animated:YES];
    }else if ([name isEqualToString:@"timeline"]){
        zzc.nameStr = headerID;
    [self.navigationController pushViewController:zzc animated:YES]
    ;
    }else if([name isEqualToString:@"tb.cn"]){
        shop.shopUrl = [NSURL URLWithString:url];
       // NSLog(@"%@",shop.shopUrl);
    [self.navigationController pushViewController:shop animated:YES
     ];
    }else if([name isEqualToString:@"ting"]){
        [ZZCMusicPlayerViewController shareMusicPlayerViewController]._id = headerID;
        NSLog(@"121@@@%@", [ZZCMusicPlayerViewController shareMusicPlayerViewController]._id);
        [[NSUserDefaults standardUserDefaults]setObject:@"lun" forKey:@"from"];
        [self.navigationController pushViewController:[ZZCMusicPlayerViewController shareMusicPlayerViewController] animated:YES];
    }else
        
        
        
    {
        detail.detaId = headerID;
        [self.navigationController pushViewController:detail animated:YES];
    }
}

#pragma mark -- 设置下拉刷新
-(void)addHeaderRefresh
{
    self.nextPage = 0;
    self.isUpLoad = NO;//标记为下拉操作
    [self requestData];
    
}
#pragma mark -- 设置上拉刷新
-(void)addFooterRefresh
{
    self.nextPage += 10;
    self.isUpLoad = YES;
    [self requestData];
}




#pragma mark - 请求数据
-(void)requestData{
    
    
    NSString *urlStr = @"http://api2.pianke.me/pub/today";
    NSString *body = [NSString stringWithFormat:@"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=10&start=%ld&version=3.0.6", self.nextPage];
    
    [ZYTools postUrl:urlStr body:body result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
        //NSLog(@"%@",result);
        if (self.isUpLoad == NO) {
            //说明是下拉，就要清空数组中的数据
            [self.allArray removeAllObjects];
        }

        
        NSMutableDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *array = [dic objectForKey:@"list"];
        for (NSMutableDictionary *allDic in array) {
            HLJHomeModel *homeMl = [[HLJHomeModel alloc]init];
            [homeMl setValuesForKeysWithDictionary:allDic];
            [self.allArray addObject:homeMl];
        }
        
        self.titleLabel.text = [dic objectForKey:@"date"];
        //把轮播图的数组取出来放数组里 传值
        self.headerView.showImgArray = [dic objectForKey:@"carousel"];
        
//        [self.boutTableView headerEndRefreshing];
//        
       // [hub hide:YES];
        
        [self.boutTableView.mj_header endRefreshing];
        [self.boutTableView.mj_footer endRefreshing];

        [self.boutTableView reloadData];
        
        
        
        
    } failure:^(NSError *error) {
        
        
    }];
    

    
    
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.allArray.count;
}

//内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //Topic 标题 图片 cell
    HLJTopicCell *topicCell = [tableView dequeueReusableCellWithIdentifier:@"topicCell"];
    //Label 没有标题  只有图片 内容 cell
    HLJLabelCell *labelCell = [tableView dequeueReusableCellWithIdentifier:@"labelCell"];
    //Sound 大播放按钮的 cell
    HLJSoundCell *soundCell = [tableView dequeueReusableCellWithIdentifier:@"soundCell"];
    //插画 illustration  三图 cell
    HLJIllustrationCell *illCell = [tableView dequeueReusableCellWithIdentifier:@"illCell"];
    //Music cell
    HLJMusicCell *musicCell = [tableView dequeueReusableCellWithIdentifier:@"musicCell"];
    //Worldcell  图 标题 内容 cell
    HLJWorldCell *worldCell = [tableView dequeueReusableCellWithIdentifier:@"worldCell"];
    
    //签协议
    illCell.illCellDeleage = self;
    

    
    
    //赋值
     HLJHomeModel *homeModel = [self.allArray objectAtIndex:indexPath.row];
    
    
    if ([homeModel.enname isEqualToString:@"Sound"]) {
        soundCell.soundModel = homeModel;
                //去除点击cell时触发的效果
        soundCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return soundCell;
        
        
        
    }else if ([homeModel.name isEqualToString:@"碎片"]){
        labelCell.labelModel = homeModel;
               labelCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return labelCell;
        
        
    }else if ([homeModel.name isEqualToString:@"音乐"]){
        
        musicCell.musicModel = homeModel;
       
        
        musicCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        return musicCell;
       
        
    }else if ([homeModel.name isEqualToString:@"插画"] || [homeModel.name isEqualToString:@"摄影"]){
        illCell.illModel = homeModel;
       
        
        illCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return illCell;
        
        
        
    }else if ([homeModel.name isEqualToString:@"话题"]){
        topicCell.topicModel = homeModel;
        
        
        topicCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        return topicCell;
        
        
        
    }else{
        worldCell.worldModel = homeModel;
        
        self.cellHeight = [HLJWorldCell heightForContent:homeModel.content] + worldCell.titleLb.origin.y + worldCell.titleLb.height + worldCell.type.origin.y/3*4 + worldCell.heartButton.height + worldCell.type.origin.y * 2;
        

               worldCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return worldCell;
    }

    
    }



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
         HLJHomeModel *homeModel = [self.allArray objectAtIndex:indexPath.row];
    
    if ([homeModel.name isEqualToString:@"插画"] || [homeModel.name isEqualToString:@"摄影"]) {
        return 540 ;
        
    }else if ([homeModel.name isEqualToString:@"音乐"]){
        return 350;
    }else if ([homeModel.enname isEqualToString:@"Sound"]){
        return 400;
    }else if ([homeModel.name isEqualToString:@"碎片"]){
        return 430;
    }else{
        return 440;
    }
}



#pragma mark - 触摸cell时触发的方法 (传值)
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    HLJDetailViewController *detailVC = [[HLJDetailViewController alloc]init];
    HLJHomeModel *homeModel = [self.allArray objectAtIndex:indexPath.row];
    if ([homeModel.enname isEqualToString:@"Sound"]) {
        
        [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"play"];
        [[NSUserDefaults standardUserDefaults]setObject:@"HomePush" forKey:@"from"];
        [ZZCMusicPlayerViewController shareMusicPlayerViewController].musicModel = homeModel;
        
        if ([[ZZCMusicPlayerViewController shareMusicPlayerViewController] pushed]) {
            [self.navigationController popToViewController:[ZZCMusicPlayerViewController shareMusicPlayerViewController] animated:YES];
        }else{
           
            [self.navigationController pushViewController:[ZZCMusicPlayerViewController shareMusicPlayerViewController] animated:YES];
            [[ZZCMusicPlayerViewController shareMusicPlayerViewController] setPushed:YES];
        }
        
    }else{
        
        detailVC.detaId = homeModel._id;
        detailVC.titleStr = homeModel.title;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
    
    
}



#pragma mark - 协议方法 (模态到相册)
-(void)presentToViewController:(NSInteger)number ImgArray:(NSMutableArray *)imgArray{
    
    HLJPhotoViewController *photoVC = [[HLJPhotoViewController alloc]init];
    
    photoVC.photoArray = imgArray;
    photoVC.photoNumber = number - 1000;
    [photoVC setModalTransitionStyle: UIModalTransitionStyleCrossDissolve];
    [self.view.window.rootViewController presentViewController:photoVC animated:YES completion:^{
        
        
        
    }];
 
    
}

#pragma mark - 请求静里的数据
-(void)getDataForJING
{
    NSString *urlStr = @"http://mapi.pianke.me/pub/jing";
    NSString *body = @"auth=W81oFCvlm3m3hDCsN8G8bskDJxQMSiNeWLoD1sXzE2cWP22N1JSh9b6P4&client=1&did=EC4A37B3-25F3-4560-BDE7-AFB0EB45C576&version=3.0.6";
    [ZYTools postUrl:urlStr body:body result:ZYJSON requestStyle:ZYRequestString headerFile:nil success:^(id result) {
        
        NSLog(@"%@", result);
        self.animouteView.jingDic = [result objectForKey:@"data"];
        
        
    } failure:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
